package cjgl.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cjgl.entity.Student;
import cjgl.entity.TbClass;
import cjgl.entity.Teacher;

import service.TeacherService;


public class TeacherDAO {
	public Teacher findById(Integer id){
		Teacher teacher=null;
		String sql="select * from teacher t where t.id="+id;
		Connection conn =  ConnectionFactory.getConnection();
		if(conn!=null) {
			Statement stmt;
			try {
				stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				if(rs.next()) {
					 teacher= new Teacher(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4));
				}
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return  teacher;
	}
	public List<Teacher> findAll(){
		List<Teacher> list= new ArrayList<Teacher>();
		String sql="select * from teacher";
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
			Statement stmt;
			try {
				stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				while(rs.next()) {
					Teacher te=new Teacher(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4));
					list.add(te);
				}
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	public void delete(Integer id) {
		String sql="delete from teacher  where id="+id;
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
			Statement stmt;
			try {
				stmt = conn.createStatement();
				stmt.execute(sql);
				conn.close();
			}catch(SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void update(Teacher teacher) {
		String sql="update teacher set name=?,password=?,username=?where id=?";
		Connection conn=ConnectionFactory.getConnection();
		if(conn!=null) {
			PreparedStatement pstmt;
			try {
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,teacher.getName());
				pstmt.setString(2,teacher.getPassword());
				pstmt.setString(3,teacher.getUsername());
				pstmt.setInt(4, teacher.getId());
				pstmt.execute();
				conn.close();
			} catch (SQLException e) {				
				e.printStackTrace();
			}			
		}
	    }
	public List<Teacher> findByTeacher(Integer id) {
		Teacher teacher=null;
		String sql="select teacher set name=?,username=?,password=?where id="+id;		
	   return null;	
			}
	
	public synchronized void insert(Teacher teacher) {
		String sql="insert into teacher values(null,?,?,?)";
		Connection conn= ConnectionFactory.getConnection();
		if(conn!=null) {
			PreparedStatement pastmt;
			try {
				pastmt = conn.prepareStatement(sql);
				pastmt.setString(1,teacher.getName());
				pastmt.setString(2,teacher.getUsername());
				pastmt.setString(3,teacher.getPassword());
				pastmt.execute();
				sql="select * from teacher t order by t.id desc";
				ResultSet rs = pastmt.executeQuery(sql);
				rs.next();
				teacher.setId(rs.getInt(1));
				conn.close();
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			
					
		}
	}
//��¼
public Teacher findByUsernameAndPassword(String username,String password) {
	Teacher teacher=null;
	String sql="select*from teacher t where t.username=? and t.password=?";
	Connection conn=ConnectionFactory.getConnection();
	PreparedStatement pstmt;
    
		try {
			 pstmt = conn.prepareStatement(sql);
             pstmt.setString(1, username);
	         pstmt.setString(2, password);
	         ResultSet rs=pstmt.executeQuery();
	         if(rs.next()) {
			    teacher=new Teacher(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
	         }
	         conn.close();
	         } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return teacher;
	}



			public static void main(String[] args) {
				TeacherDAO teacherDAO=new TeacherDAO();
				//���
				//Teacher teacher=new Teacher(null,"name","aaa","1110");
				//teacherDAO.insert(teacher);
				List<Teacher> list=teacherDAO.findAll();
				System.out.println(list);
			    Teacher tea=teacherDAO.findById(1);
			    tea.setName("dfaafdfasf");
			    teacherDAO.update(tea);
			    teacherDAO.delete(1);
				List<Teacher>list1 = teacherDAO.findAll();
				System.out.println(list1);
				
			}
			public Teacher login1(String username, String pwd) {
				Teacher model = null;
				String sql = "select * from teacher t where t.username=? and t.password=?";
				Connection con = ConnectionFactory.getConnection();
				if(con!=null) {
					PreparedStatement ps;
					try {
						ps = con.prepareStatement(sql);
						ps.setString(1, username);
						ps.setString(2, pwd);
						ResultSet rs = ps.executeQuery();
						if(rs.next()) {
							model = new Teacher(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
						}
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
				}
				}
				return model;
			}

			}
			
			


